package com.canbot.u05.activity.shoppingGuide.codeMarketing;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.canbot.u05.R;
import com.canbot.u05.glide.GlideApp;
import com.canbot.u05.utils.ToastTools;
import com.canbot.u05.utils.image.ImageDao;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author zmp
 * 导览模式选择底图界面
 */
public class CodeChoosePicActivity extends BaseCodeFinishActivity implements AdapterView.OnItemClickListener {

        @BindView(R.id.code_iv_back)
        Button codeIvBack;

        @BindView(R.id.code_iv_next)
        Button codeIvNext;

        @BindView(R.id.choose_pic_gv)
        GridView choosePicGv;

        private List<String> picPaths;

        private boolean[] bs;

        private int currentPosition;

        private ToastTools toastTools;

        private String picPath;

        private boolean isNext;

        private static final int Q_VOICE_CODE = 1001;


        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_code_choose_pic);
                ButterKnife.bind(this);
                isNext = getIntent().getBooleanExtra("isNext", false);
                initView();
                initData();
        }

        @Override
        protected void onNewIntent(Intent intent) {
                super.onNewIntent(intent);
                isNext = intent.getBooleanExtra("isNext", false);
        }

        private void initData() {
                currentPosition = -1;
                picPaths = ImageDao.getAllImagePath(this);
                if (picPaths == null || picPaths.size() == 0) {
                        toastTools.showText(getString(R.string.no_optional_background));
                        return;
                }
                bs = new boolean[picPaths.size()];
                if (!TextUtils.isEmpty(picPath)) {
                        currentPosition = -1;
                        for (int i = 0; i < picPaths.size(); i++) {
                                if (picPath.contains(picPaths.get(i))) {
                                        currentPosition = i;
                                        bs[currentPosition] = true;
                                        break;
                                }
                        }
                }
                myBaseAdapter.notifyDataSetChanged();
        }

        private void initView() {
                toastTools = ToastTools.newInstance(this);
                choosePicGv.setAdapter(myBaseAdapter);
                choosePicGv.setOnItemClickListener(this);
                picPath = getIntent().getStringExtra("picPath");
        }


        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Log.e("test", "onItemClick" + position);
                if (currentPosition != -1) {
                        bs[currentPosition] = false;
                }
                currentPosition = position;
                bs[position] = true;
                myBaseAdapter.notifyDataSetChanged();
        }


        BaseAdapter myBaseAdapter = new BaseAdapter() {
                @Override
                public int getCount() {
                        return picPaths == null ? 0 : picPaths.size();
                }

                @Override
                public Object getItem(int position) {
                        return position;
                }

                @Override
                public long getItemId(int position) {
                        return position;
                }

                @Override
                public View getView(int position, View convertView, ViewGroup parent) {
                        if (convertView == null) {
                                LayoutInflater inflater = LayoutInflater.from(CodeChoosePicActivity.this);
                                convertView = inflater.inflate(R.layout.guide_choose_pic_item, parent, false);
                                Holder holder = new Holder(convertView);
                                convertView.setTag(holder);
                        }
                        Holder holder = ((Holder) convertView.getTag());
                        holder.setData(position);
                        return convertView;
                }
        };

        @OnClick({R.id.code_iv_back, R.id.code_iv_next})
        public void onViewClicked(View view) {
                switch (view.getId()) {
                        case R.id.code_iv_back:
                                finish();
                                break;
                        case R.id.code_iv_next:
                                if (currentPosition == -1) {
                                        toastTools.showText(getString(R.string.select_base_map));
                                        return;
                                }
//                                if (isNext) {
//                                        CodeDataHelper.getInstance().getCodeMarketingInfo().setImagePath(picPaths.get(currentPosition));
//                                        Intent intent = new Intent(this, HostModeCustomModuleActivity.class);
//                                        intent.putExtra("isEnableNull", true);
//                                        startForResultActivity(intent, Q_VOICE_CODE);
//                                        return;
//                                }
                                Intent intent = new Intent().putExtra("picPath", picPaths.get(currentPosition));
                                setResult(RESULT_OK, intent);
                                finish();
                                break;
                }
        }

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
                super.onActivityResult(requestCode, resultCode, data);
                if (requestCode == Q_VOICE_CODE && resultCode == RESULT_OK) {
                        int type = data.getIntExtra("type", -1);
                        if (type == -1) {
                                CodeDataHelper.getInstance().getCodeMarketingInfo().setVoiceType(-1);
                                CodeDataHelper.getInstance().getCodeMarketingInfo().setVoiceContent(null);
                                return;
                        }
                        String path = data.getStringExtra("path");
                        CodeDataHelper.getInstance().getCodeMarketingInfo().setVoiceType(type);
                        CodeDataHelper.getInstance().getCodeMarketingInfo().setVoiceContent(path);
                        startOtherActivity(CodeTimeActivity.class, false);
                }
        }

        class Holder {

                private final TextView textView;

                private final ImageView checkIv;

                private final ImageView imageView;

                Holder(View inflate) {
                        textView = (TextView) inflate.findViewById(R.id.tv);
                        checkIv = (ImageView) inflate.findViewById(R.id.cb);
                        imageView = (ImageView) inflate.findViewById(R.id.iv);
                }

                public void setData(int position) {
                        textView.setText(picPaths.get(position));
                        checkIv.setVisibility(bs[position] ? View.VISIBLE : View.GONE);
                        checkIv.setTag(position);
                        checkIv.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                        int tag = (Integer) checkIv.getTag();
                                        if (currentPosition == tag && bs[currentPosition]) {
                                                bs[currentPosition] = false;
                                                currentPosition = -1;
                                                myBaseAdapter.notifyDataSetChanged();
                                                return;
                                        }
                                        if (currentPosition != -1) {
                                                bs[currentPosition] = false;
                                        }
                                        currentPosition = tag;
                                        bs[currentPosition] = true;
                                        myBaseAdapter.notifyDataSetChanged();
                                }
                        });
                        GlideApp.with(CodeChoosePicActivity.this).clear(imageView);
                        GlideApp.with(CodeChoosePicActivity.this)
                                .asDrawable()
                                .load(picPaths.get(position).replace("/uupic/","/uupic_thumb/"))
                                .thumbnail(0.1f)
                                .diskCacheStrategy(DiskCacheStrategy.ALL)//缓存策略
                                .skipMemoryCache(false)
                                .placeholder(R.drawable.sales_banner01)//占位图
                                .error(R.drawable.sales_banner01)
                                .override(100, 100)
                                .into(imageView);
                }
        }
}
